﻿Imports System.IO
Imports System.ComponentModel
Imports System.Threading
Imports System.Text.RegularExpressions


Public Class MainWindow
    Delegate Sub AddListItem(myString As String)
    Public myDelegate As AddListItem

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.FolderBrowserDialog1.SelectedPath = Me.TextBox1.Text
        If Me.FolderBrowserDialog1.ShowDialog() Then
            Me.TextBox1.Text = Me.FolderBrowserDialog1.SelectedPath
        End If

    End Sub

    Private Sub startstop_Click(sender As Object, e As EventArgs) Handles startstop.Click
        Dim di As New IO.DirectoryInfo(Me.TextBox1.Text)

        Dim diar1 As IO.FileInfo() = di.GetFiles("combat_????-??-??_??_??_??_??????.txt")
        Dim dra As IO.FileInfo
        'Dim fileregex As New Regex()

        'list the names of all files in the specified directory
        For Each dra In diar1
            Me.TextBox2.Text += dra.ToString & vbCrLf
        Next



        Dim myThreadClassObject As New MyThreadClass(Me)
        myThreadClassObject.Run()
    End Sub

    Public Sub AddListItemMethod(myString As String)
        Me.TextBox2.Text += myString & vbCrLf

    End Sub 'AddListItemMethod


    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        myDelegate = New AddListItem(AddressOf AddListItemMethod)
    End Sub

    Private Sub BackgroundWorker1_DoWork(sender As Object, e As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
        '
    End Sub
End Class

Public Class MyThreadClass
    Dim myFormControl1 As MainWindow
    Dim fileWatcher As New System.IO.FileSystemWatcher


    Public Sub New(myForm As MainWindow)
        myFormControl1 = myForm
    End Sub 'New
    Private myString As String

    Public Sub Run()
        If fileWatcher.EnableRaisingEvents = False Then

            fileWatcher.Path = myFormControl1.TextBox1.Text
            AddHandler fileWatcher.Changed, AddressOf OnChanged
            AddHandler fileWatcher.Created, AddressOf OnChanged
            AddHandler fileWatcher.Deleted, AddressOf OnChanged
            AddHandler fileWatcher.Renamed, AddressOf OnRenamed
            fileWatcher.EnableRaisingEvents = True
        Else
            fileWatcher.EnableRaisingEvents = False
        End If
    End Sub 'Run

    ' Define the event handlers.
    Private Sub OnChanged(source As Object, e As FileSystemEventArgs)
        ' Specify what is done when a file is changed, created, or deleted.
        myString = "File: " & e.FullPath & " " & e.ChangeType
        myFormControl1.Invoke(myFormControl1.myDelegate, New Object() {myString})
    End Sub

    Private Sub OnRenamed(source As Object, e As RenamedEventArgs)
        ' Specify what is done when a file is renamed.
        myString = "File: " & e.OldFullPath & " renamed to " & e.FullPath
        myFormControl1.Invoke(myFormControl1.myDelegate, New Object() {myString})
    End Sub

End Class 'MyThreadClass

